package com.songtech.uitest.module.common;

import com.songtech.uitest.util.AutoWait;
import com.songtech.uitest.util.Space;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AlertHandler {

    private Logger logger = LoggerFactory.getLogger(AlertHandler.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    public boolean checkAndGo(WebDriver driver) throws Exception{
        try{
            WebElement div = AutoWait.findByCss("div.modal.fade.in");
            String info = AutoWait.findByCss(div, "div.modal-body").getText();
            logger.info(space.getSpace() + "弹出提示框信息：" + info);
            //若影响流程的提交失败出现，则当前步骤要触发重做
            if(info.contains("失败") || info.contains("处理过了")){
                throw new Exception("弹出框提示当前操作失败：" + info);
            }

            AutoWait.findByXpath("//button[text()='关闭']").click();
            return true;

        }
        catch (NoSuchElementException e){
            logger.info(space.getSpace() + "检测到提示框没能正确弹出");
            return false;
        }


    }

}
